package entity;


import java.text.SimpleDateFormat;
import java.util.*;

/***
 * 话题对象
 */

public class Feed{

    private static final double INVALID_LNG_LAT_VALUE = -1000D;

    //活动ID由服务端产生
    private Long id;

    //话题标题
    private String caption;

    //话题内容
    private String content;

    //发布时间
    private long publishTime;

    //开始时间
    private long startTime;

    //发布者ID
    private long ownerId;

    //发布图片，最多九张
    private String imgUri_1;

    private String imgUri_2;

    private String imgUri_3;

    private String imgUri_4;

    private String imgUri_5;

    private String imgUri_6;

    private String imgUri_7;

    private String imgUri_8;

    private String imgUri_9;

    //经度
    private Double lng;

    //维度
    private Double lat;

    //地址信息
    private String locationStr;

    //发布者
    private User owner;



    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCaption() {
        return this.caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getContent() {
        return this.content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getPublishTime() {
        return this.publishTime;
    }

    public void setPublishTime(long publishTime) {
        this.publishTime = publishTime;
    }

    public long getStartTime() {
        return this.startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getOwnerId() {
        return this.ownerId;
    }

    public void setOwnerId(long ownerId) {
        this.ownerId = ownerId;
    }

    String getImgUri_1() {
        return this.imgUri_1;
    }

    void setImgUri_1(String imgUri_1) {
        this.imgUri_1 = imgUri_1;
    }

    String getImgUri_2() {
        return this.imgUri_2;
    }

    void setImgUri_2(String imgUri_2) {
        this.imgUri_2 = imgUri_2;
    }

    String getImgUri_3() {
        return this.imgUri_3;
    }

    void setImgUri_3(String imgUri_3) {
        this.imgUri_3 = imgUri_3;
    }

    String getImgUri_4() {
        return this.imgUri_4;
    }

    void setImgUri_4(String imgUri_4) {
        this.imgUri_4 = imgUri_4;
    }

    String getImgUri_5() {
        return this.imgUri_5;
    }

    void setImgUri_5(String imgUri_5) {
        this.imgUri_5 = imgUri_5;
    }

    String getImgUri_6() {
        return this.imgUri_6;
    }

    void setImgUri_6(String imgUri_6) {
        this.imgUri_6 = imgUri_6;
    }

    String getImgUri_7() {
        return this.imgUri_7;
    }

    void setImgUri_7(String imgUri_7) {
        this.imgUri_7 = imgUri_7;
    }

    String getImgUri_8() {
        return this.imgUri_8;
    }

    void setImgUri_8(String imgUri_8) {
        this.imgUri_8 = imgUri_8;
    }

    String getImgUri_9() {
        return this.imgUri_9;
    }

    void setImgUri_9(String imgUri_9) {
        this.imgUri_9 = imgUri_9;
    }

    public Double getLng() {
        return this.lng;
    }

    public void setLng(Double lng) {
        this.lng = lng;
    }

    public Double getLat() {
        return this.lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User u) {
        owner = u;
    }


    public List<String> getImageUriList() {
        List<String> list = new ArrayList<String>();
        if (imgUri_1 != null) list.add(imgUri_1);
        if (imgUri_2 != null) list.add(imgUri_2);
        if (imgUri_3 != null) list.add(imgUri_3);
        if (imgUri_4 != null) list.add(imgUri_4);
        if (imgUri_5 != null) list.add(imgUri_5);
        if (imgUri_6 != null) list.add(imgUri_6);
        if (imgUri_7 != null) list.add(imgUri_7);
        if (imgUri_8 != null) list.add(imgUri_8);
        if (imgUri_9 != null) list.add(imgUri_9);
        return list;
    }

    public String getStartTimeStr() {

        Calendar today = Calendar.getInstance();
        today.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        today.set(Calendar.MILLISECOND, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.HOUR, 0);

        SimpleDateFormat sdf;
        if (startTime >= today.getTimeInMillis() + 86400000L)
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        else
            sdf = new SimpleDateFormat("HH:mm aa", Locale.CHINA);

        return sdf.format(startTime);

    }

    public boolean isFeedClosed() {
        return System.currentTimeMillis() > startTime;
    }

    public String getPublishTimeStr() {

        Calendar now = Calendar.getInstance();
        Calendar ago = Calendar.getInstance();
        ago.setTimeInMillis(publishTime);

        //一天内
        long diff = System.currentTimeMillis() - publishTime;
        if (diff <= 86400000L) {
            if (now.get(Calendar.DATE) != ago.get(Calendar.DATE))
                return "昨天";
            if (diff >= 3600000L)
                return String.format(Locale.CHINA, "%d小时前", diff / 3600000L);
            if (diff >= 60000L)
                return String.format(Locale.CHINA, "%d分钟前", diff / 60000L);
            return "1分钟内";
        }

        now.set(Calendar.MILLISECOND, 0);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.HOUR, 0);
        if (now.getTimeInMillis() - publishTime <= 86400000L)
            return "昨天";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf.format(ago.getTime());
    }

    public int getImageCount() {
        if (imgUri_1 == null) return 0;
        if (imgUri_2 == null) return 1;
        if (imgUri_3 == null) return 2;
        if (imgUri_4 == null) return 3;
        if (imgUri_5 == null) return 4;
        if (imgUri_6 == null) return 5;
        if (imgUri_7 == null) return 6;
        if (imgUri_8 == null) return 7;
        if (imgUri_9 == null) return 8;
        return 9;
    }

    public void clearImageUris() {
        imgUri_1 = null;
        imgUri_2 = null;
        imgUri_3 = null;
        imgUri_4 = null;
        imgUri_5 = null;
        imgUri_6 = null;
        imgUri_7 = null;
        imgUri_8 = null;
        imgUri_9 = null;
    }

    public void setImageUri(int position, String uri) {
        switch (position) {
            case 8:
                imgUri_9 = uri;
                break;
            case 7:
                imgUri_8 = uri;
                break;
            case 6:
                imgUri_7 = uri;
                break;
            case 5:
                imgUri_6 = uri;
                break;
            case 4:
                imgUri_5 = uri;
                break;
            case 3:
                imgUri_4 = uri;
                break;
            case 2:
                imgUri_3 = uri;
                break;
            case 1:
                imgUri_2 = uri;
                break;
            default:
                imgUri_1 = uri;
                break;
        }
    }



    public String getLocationStr() {
        return this.locationStr;
    }

    public void setLocationStr(String locationStr) {
        this.locationStr = locationStr;
    }



    public Feed(Long id, String caption, String content,
                long publishTime, long startTime, long ownerId, String imgUri_1,
                String imgUri_2, String imgUri_3, String imgUri_4, String imgUri_5,
                String imgUri_6, String imgUri_7, String imgUri_8, String imgUri_9,
                Double lng, Double lat, String locationStr) {
        this.id = id;
        this.caption = caption;
        this.content = content;
        this.publishTime = publishTime;
        this.startTime = startTime;
        this.ownerId = ownerId;
        this.imgUri_1 = imgUri_1;
        this.imgUri_2 = imgUri_2;
        this.imgUri_3 = imgUri_3;
        this.imgUri_4 = imgUri_4;
        this.imgUri_5 = imgUri_5;
        this.imgUri_6 = imgUri_6;
        this.imgUri_7 = imgUri_7;
        this.imgUri_8 = imgUri_8;
        this.imgUri_9 = imgUri_9;
        this.lng = lng;
        this.lat = lat;
        this.locationStr = locationStr;
    }

    public Feed() {
    }
}